package com.controller;

import com.common.annotation.Log;
import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.core.page.TableDataInfo;
import com.common.enums.BusinessType;
import com.common.utils.poi.ExcelUtil;
import com.domain.dto.KycxDTO;
import com.domain.param.KycxParam;
import com.domain.vo.KycxLwVo;
import com.domain.vo.KycxVo;
import com.domain.vo.KycxZZVo;
import com.domain.vo.KycxZlVo;
import com.service.IKycxLwService;
import com.service.IKycxZlService;
import com.service.IKycxZzService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * PaperController   ---- 删除和详情接口参数加了type
 * 成果管理，科研成果，学术论文
 */

@RestController
@RequestMapping("/kycx")
@Api(tags = "科研诚信模块")
public class KycxController extends BaseController {

    @Autowired
    private IKycxLwService lwService;
    @Autowired
    private IKycxZlService zlService;
    @Autowired
    private IKycxZzService zzService;

    /**
     * 列表检索
     */
    @Log(title = "列表检索")
    @ApiOperation(value = "列表检索", notes = "列表检索")
    @GetMapping("/list")
    public TableDataInfo list(@Valid KycxParam param) {
        startPage();
        Integer type = param.getType();
        List<KycxVo> list = null;
        switch (type) {
            case 1:
                list = lwService.selectByPage(param);
                break;
            case 2:
                list = zlService.selectByPage(param);
                break;
            case 3:
                list = zzService.selectByPage(param);
                break;
        }

        return getDataTable(list);
    }


    @Log(title = "导出", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出", notes = "导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody @Valid KycxParam param) {
        Integer type = param.getType();
        switch (type) {
            case 1:
                List<KycxLwVo> list_lw = lwService.selectByExport_lw(param);
                List<String> selectedColumns_lw = param.getExcelColumn();
                ExcelUtil<KycxLwVo> util_lw = new ExcelUtil<>(KycxLwVo.class);
                util_lw.showColumn(selectedColumns_lw.toArray(new String[0]));
                util_lw.exportExcelByOrder(response, list_lw, "论文清单", selectedColumns_lw.toArray(new String[0]));
                break;
            case 2:
                List<KycxZlVo> list_zl = zlService.selectByExport_zl(param);
                List<String> selectedColumns_zl = param.getExcelColumn();
                ExcelUtil<KycxZlVo> util_zl = new ExcelUtil<>(KycxZlVo.class);
                util_zl.showColumn(selectedColumns_zl.toArray(new String[0]));
                util_zl.exportExcelByOrder(response, list_zl, "专利清单", selectedColumns_zl.toArray(new String[0]));
                break;
            case 3:
                List<KycxZZVo> list_zz = zzService.selectByExport_zz(param);
                List<String> selectedColumns_zz = param.getExcelColumn();
                ExcelUtil<KycxZZVo> util_zz = new ExcelUtil<>(KycxZZVo.class);
                util_zz.showColumn(selectedColumns_zz.toArray(new String[0]));
                util_zz.exportExcelByOrder(response, list_zz, "著作清单", selectedColumns_zz.toArray(new String[0]));
                break;
        }

    }

    /**
     * 左上方统计 通过，退回，待审核
     *
     * @param param 检索参数
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('achievement:paper:list')")
    @ApiOperation(value = "左上方统计 通过，退回，待审核", notes = "左上方统计 通过，退回，待审核")
    @GetMapping(value = "/list-statistics")
    public AjaxResult listStatistics(KycxParam param) {
        Integer type = param.getType();
        List<HashMap<String, Integer>> list = null;
        switch (type) {
            case 1:
                list = lwService.listStatistics(param);
                break;
            case 2:
                list = zlService.listStatistics(param);
                break;
            case 3:
                list = zzService.listStatistics(param);
                break;
        }

        return success(list);
    }

    /**
     * 获取详细信息
     */
    @ApiOperation(value = "获取详细信息", notes = "获取详细信息")
    @GetMapping(value = "/{id}/{type}")
    public AjaxResult getInfo(@PathVariable("id") String id, @PathVariable("type") Integer type) {
        KycxVo kycx = null;
        switch (type) {
            case 1:
                kycx = lwService.getInfoById(id);
                break;
            case 2:
                kycx = zlService.getInfoById(id);
                break;
            case 3:
                kycx = zzService.getInfoById(id);
                break;
        }
        return success(kycx);

    }

    /**
     * 新增
     */

    @Log(title = "新增基本信息及作者信息", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增基本信息及作者信息", notes = "新增基本信息及作者信息")
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody @Valid KycxDTO dto) {
        Integer type = dto.getType();
        AjaxResult add = null;
        switch (type) {
            case 1:
                add = lwService.add(dto);
                break;
            case 2:
                add = zlService.add(dto);
                break;
            case 3:
                add = zzService.add(dto);
                break;
        }
        return add;
    }

    /**
     * 删除
     */

    @Log(title = "删除论文", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除", notes = "删除论文")
    @DeleteMapping("/del/{ids}/{type}")
    public AjaxResult remove(@PathVariable List<String> ids, @PathVariable("type") Integer type) {
        Boolean bl = null;
        switch (type) {
            case 1:
                bl = lwService.batchRemove(ids);
                break;
            case 2:
                bl = zlService.batchRemove(ids);
                break;
            case 3:
                bl = zzService.batchRemove(ids);
                break;
        }
        return toAjax(bl);
    }

    /**
     * 修改
     */

    @Log(title = "修改论文", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改", notes = "修改论文")
    @PostMapping(value = "/update")
    public AjaxResult edit(@RequestBody KycxDTO dto) {
        Integer type = dto.getType();
        AjaxResult update = null;
        switch (type) {
            case 1:
                update = lwService.update(dto);
                break;
            case 2:
                update = zlService.update(dto);
                break;
            case 3:
                update = zzService.update(dto);
                break;
        }
        return update;
    }


}
